package ar.uba.dc.webming.tp1.measures;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopScoreDocCollector;

import ar.uba.dc.webming.tp1.relevance.RelevanceJudgementMap;



public class ResultSetEvaluator {
    private Query query;
    private Searcher searcher;
    private RelevanceJudgementMap rjMap;
    private String queryNum;
    private ScoreDoc[] hits;
    
    public ResultSetEvaluator(Searcher searcher, Query query, RelevanceJudgementMap rjMap, String queryNum) throws IOException{
        this.query = query;
        this.searcher = searcher;
        this.rjMap = rjMap;
        this.queryNum = queryNum;
        TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
        searcher.search(query, collector);
        int totalHits = collector.getTotalHits();
        collector = TopScoreDocCollector.create(totalHits, false);
        searcher.search(query, collector);
        hits = collector.topDocs().scoreDocs;
    }
    
    
    
    public Query getQuery() {
        return query;
    }



    public Searcher getSearcher() {
        return searcher;
    }



    public RelevanceJudgementMap getRjMap() {
        return rjMap;
    }



    public String getQueryNum() {
        return queryNum;
    }



    public Float calculateRecall() throws IOException{
//        TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
//        searcher.search(query, collector);
//        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        int relevantItems = rjMap.get(queryNum).size();
        int relevantItemsRetrieved = 0;
        for (int i = 0; i < hits.length; i++) {
            Document doc = searcher.doc(hits[i].doc);
            String ui = doc.get(".U");
            if(ui!=null){
                if(rjMap.isRelevantForQuery(queryNum, ui)){
                    relevantItemsRetrieved++;
                }
            }
        }
        if(relevantItems!=0) {
            return Float.valueOf(relevantItemsRetrieved)/Float.valueOf(relevantItems);
        }else{
            return Float.valueOf(0);
        }
    }
    
    
    public Float calculatePrecision() throws IOException{
//        TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
//        searcher.search(query, collector);
//        ScoreDoc[] hits = collector.topDocs().scoreDocs;
//        int retrievedItems = collector.getTotalHits();
        int relevantItemsRetrieved = 0;
        for (int i = 0; i < hits.length; i++) {
            Document doc = searcher.doc(hits[i].doc);
            String ui = doc.get(".U");
            if(ui!=null){
                if(rjMap.isRelevantForQuery(queryNum, ui)){
                    relevantItemsRetrieved++;
                }
            }
         }
        if(hits.length!=0) {
            return Float.valueOf(relevantItemsRetrieved)/Float.valueOf(hits.length);
        }else{
            return Float.valueOf(0);
        }
    }
    
    public Float calculateRPrecision() throws IOException{
//        TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
//        searcher.search(query, collector);
//        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        int relevantItems = rjMap.get(queryNum).size();
        int relevantItemsRetrieved = 0;
        for (int i = 0; i < Math.min(relevantItems, hits.length); i++) {
            Document doc = searcher.doc(hits[i].doc);
            String ui = doc.get(".U");
            if(ui!=null){
                if(rjMap.isRelevantForQuery(queryNum, ui)){
                    relevantItemsRetrieved++;
                }
            }
        }
        if(relevantItems!=0) {
            return Float.valueOf(relevantItemsRetrieved)/Float.valueOf(relevantItems);
        }else{
            return Float.valueOf(0);
        }
    }



    public int totalResults() throws IOException {
//        TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
//        searcher.search(query, collector);
//        ScoreDoc[] hits = collector.topDocs().scoreDocs;
//        return collector.getTotalHits();
        return hits.length;
    }
    
    public int relevantItems(){
        return rjMap.get(queryNum).size();
    }
    
    public int relItemsRetrieved() throws IOException{
        TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
        searcher.search(query, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        int relevantItemsRetrieved = 0;
        for (int i = 0; i < hits.length; i++) {
            Document doc = searcher.doc(hits[i].doc);
            String ui = doc.get(".U");
            if(ui!=null){
                if(rjMap.isRelevantForQuery(queryNum, ui)){
                    relevantItemsRetrieved++;
                }
            }
         }
        return relevantItemsRetrieved;
    }
}
